﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using PlayDeezGames.Common.Names;

namespace PlayDeezGames.Common
{
    public class StringProperty : Property
    {
        public class StringPropertyPropertyFactory : IPropertyFactory
        {
            public object LoadFromXElement(System.Xml.Linq.XElement theXElement)
            {
                return (object)theXElement.Value;
            }

            public void SaveToXElement(XElement theXElement, object theObject)
            {
                theXElement.Value = theObject as string;
            }

            private StringPropertyPropertyFactory()
            {
            }
            public static void Register()
            {
                PropertyFactories.RegisterFactory(Names.CommonTypes.String, new StringPropertyPropertyFactory(),Names.CommonFactories.StringPropertyFactory);
            }
            public static void Unregister()
            {
                PropertyFactories.UnregisterFactory(Names.CommonTypes.String);
            }


            public object CreateNew()
            {
                return string.Empty;
            }
        }

        public class StringPropertyFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new StringProperty(theParent as Descriptor, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new StringProperty(theParent as Descriptor);
            }
            private StringPropertyFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.CommonFactories.StringPropertyFactory, new StringProperty.StringPropertyFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.CommonFactories.StringPropertyFactory);
            }
        }

        protected StringProperty(Descriptor parent)
            : base(parent)
        {
            Type = CommonTypes.String;
        }
        protected StringProperty(Descriptor parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = base.OnToXElement();
            result.Attribute(PlayDeezGames.Common.Names.CommonXNames.Factory).Value = Names.CommonFactories.StringPropertyFactory;
            return result;
        }

        public string Value
        {
            get
            {
                return (string)Payload;
            }
            set
            {
                Payload = value;
            }
        }

        public static new StringProperty Create(Descriptor theParent)
        {
            return EntityFactories.Create(theParent, Names.CommonFactories.StringPropertyFactory) as StringProperty;
        }

        public static new StringProperty Load(Descriptor theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as StringProperty;
        }
    }

    public class IntProperty : Property
    {
        public class IntPropertyPropertyFactory : IPropertyFactory
        {
            public object LoadFromXElement(System.Xml.Linq.XElement theXElement)
            {
                return (object)int.Parse(theXElement.Value);
            }

            public void SaveToXElement(XElement theXElement, object theObject)
            {
                int theInt = int.Parse(theObject.ToString());
                theXElement.Value = theInt.ToString();
            }
            private IntPropertyPropertyFactory()
            {
            }
            public static void Register()
            {
                PropertyFactories.RegisterFactory(Names.CommonTypes.Int, new IntPropertyPropertyFactory(),Names.CommonFactories.IntPropertyFactory);
            }
            public static void Unregister()
            {
                PropertyFactories.UnregisterFactory(Names.CommonTypes.Int);
            }


            public object CreateNew()
            {
                return 0;
            }
        }
        public class IntPropertyFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new IntProperty(theParent as Descriptor, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new IntProperty(theParent as Descriptor);
            }
            private IntPropertyFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.CommonFactories.IntPropertyFactory, new IntProperty.IntPropertyFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.CommonFactories.IntPropertyFactory);
            }
        }

        protected IntProperty(Descriptor parent)
            : base(parent)
        {
            Type = CommonTypes.Int;
        }
        protected IntProperty(Descriptor parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = base.OnToXElement();
            result.Attribute(PlayDeezGames.Common.Names.CommonXNames.Factory).Value = Names.CommonFactories.IntPropertyFactory;
            return result;
        }
        public int Value
        {
            get
            {
                return (int)Payload;
            }
            set
            {
                Payload = value;
            }
        }
        public static new IntProperty Create(Descriptor theParent)
        {
            return EntityFactories.Create(theParent, Names.CommonFactories.IntPropertyFactory) as IntProperty;
        }
        public static new IntProperty Load(Descriptor theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as IntProperty;
        }
    }

    public class DoubleProperty : Property
    {
        public class DoublePropertyPropertyFactory : IPropertyFactory
        {
            public object LoadFromXElement(System.Xml.Linq.XElement theXElement)
            {
                return (object)double.Parse(theXElement.Value);
            }

            public void SaveToXElement(XElement theXElement, object theObject)
            {
                double theDouble = double.Parse(theObject.ToString());
                theXElement.Value = theDouble.ToString();
            }
            private DoublePropertyPropertyFactory()
            {
            }
            public static void Register()
            {
                PropertyFactories.RegisterFactory(Names.CommonTypes.Double, new DoublePropertyPropertyFactory(),Names.CommonFactories.DoublePropertyFactory);
            }
            public static void Unregister()
            {
                PropertyFactories.UnregisterFactory(Names.CommonTypes.Double);
            }


            public object CreateNew()
            {
                return double.NaN;
            }
        }
        public class DoublePropertyFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new DoubleProperty(theParent as Descriptor, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new DoubleProperty(theParent as Descriptor);
            }
            private DoublePropertyFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.CommonFactories.DoublePropertyFactory, new DoubleProperty.DoublePropertyFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.CommonFactories.DoublePropertyFactory);
            }
        }

        protected DoubleProperty(Descriptor parent)
            : base(parent)
        {
            Type = CommonTypes.Double;
        }
        protected DoubleProperty(Descriptor parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = base.OnToXElement();
            result.Attribute(PlayDeezGames.Common.Names.CommonXNames.Factory).Value = Names.CommonFactories.DoublePropertyFactory;
            return result;
        }
        public double Value
        {
            get
            {
                return (double)Payload;
            }
            set
            {
                Payload = value;
            }
        }
        public static new DoubleProperty Create(Descriptor theParent)
        {
            return EntityFactories.Create(theParent, Names.CommonFactories.DoublePropertyFactory) as DoubleProperty;
        }
        public static new DoubleProperty Load(Descriptor theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as DoubleProperty;
        }
    }

    public class BoolProperty : Property
    {
        public class BoolPropertyPropertyFactory : IPropertyFactory
        {
            public object LoadFromXElement(System.Xml.Linq.XElement theXElement)
            {
                return (object)bool.Parse(theXElement.Value);
            }

            public void SaveToXElement(XElement theXElement, object theObject)
            {
                bool theBool = bool.Parse(theObject.ToString());
                theXElement.Value = theBool.ToString();
            }
            private BoolPropertyPropertyFactory()
            {
            }
            public static void Register()
            {
                PropertyFactories.RegisterFactory(Names.CommonTypes.Bool, new BoolPropertyPropertyFactory(),Names.CommonFactories.BoolPropertyFactory);
            }
            public static void Unregister()
            {
                PropertyFactories.UnregisterFactory(Names.CommonTypes.Bool);
            }


            public object CreateNew()
            {
                return false;
            }
        }
        public class BoolPropertyFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new BoolProperty(theParent as Descriptor, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new BoolProperty(theParent as Descriptor);
            }
            private BoolPropertyFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.CommonFactories.BoolPropertyFactory, new BoolProperty.BoolPropertyFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.CommonFactories.BoolPropertyFactory);
            }
        }

        protected BoolProperty(Descriptor parent)
            : base(parent)
        {
            Type = CommonTypes.Bool;
        }
        protected BoolProperty(Descriptor parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = base.OnToXElement();
            result.Attribute(PlayDeezGames.Common.Names.CommonXNames.Factory).Value = Names.CommonFactories.BoolPropertyFactory;
            return result;
        }
        public bool Value
        {
            get
            {
                return (bool)Payload;
            }
            set
            {
                Payload = value;
            }
        }
        public static new BoolProperty Create(Descriptor theParent)
        {
            return EntityFactories.Create(theParent, Names.CommonFactories.BoolPropertyFactory) as BoolProperty;
        }
        public static new BoolProperty Load(Descriptor theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as BoolProperty;
        }
    }


}
